package com.tsd.common.controller;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.entity.SysUserExt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * 描述：通用工具型方法 控制层
 * 包括服务器时间、计算某个时间到现在的分钟数
 *
 * @author huawei
 * @date 2022/12/26 10:08:20
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "通用工具型方法", description = "")
public class CommonUtilController extends BaseController {

    @ApiOperation(value = "根据格式返回当前时间字符串", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "format", value = "时间记录", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getNowStrWithFormat.action", method = RequestMethod.POST)
    public HttpResult<String> getNowStrWithFormat() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String format = HlpUtils.getString(map, "format", HlpUtils.Format_Yyyy_MM_dd_HH_mm_ss);
            String s = HlpUtils.date2String(new Date(), format);
            hr.setData(s);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "返回从某个基准时间开始的毫秒数", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "base_time", value = "基准时间字符串", required = true, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "format", value = "基准时间格式，不指定则为：Yyyy_MM_dd_HH_mm_ss", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getMillisFromBaseTime.action", method = RequestMethod.POST)
    public HttpResult<Long> getMillisFromBaseTime() {
        HttpResult<Long> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String base_time = HlpUtils.getString(map, "base_time");
            String format = HlpUtils.getString(map, "format", HlpUtils.Format_Yyyy_MM_dd_HH_mm_ss);
            Date base = HlpUtils.string2Date(base_time, format);
            long ms = System.currentTimeMillis() - base.getTime();
            hr.setData(ms);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
